package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.WbProductAchievement;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.WbProductEquipmentBo;
import org.dromara.system.domain.vo.WbProductEquipmentVo;
import org.dromara.system.domain.WbProductEquipment;
import org.dromara.system.mapper.WbProductEquipmentMapper;
import org.dromara.system.service.IWbProductEquipmentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 生产设备Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-25
 */
@RequiredArgsConstructor
@Service
public class WbProductEquipmentServiceImpl implements IWbProductEquipmentService {

    private final WbProductEquipmentMapper baseMapper;

    /**
     * 查询生产设备
     */
    @Override
    public WbProductEquipmentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询生产设备列表
     */
    @Override
    public TableDataInfo<WbProductEquipmentVo> queryPageList(WbProductEquipmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WbProductEquipment> lqw = buildQueryWrapper(bo);
        Page<WbProductEquipmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询生产设备列表
     */
    @Override
    public List<WbProductEquipmentVo> queryList(WbProductEquipmentBo bo) {
        LambdaQueryWrapper<WbProductEquipment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WbProductEquipment> buildQueryWrapper(WbProductEquipmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WbProductEquipment> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyId() != null, WbProductEquipment::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getCompanyName()), WbProductEquipment::getCompanyName, bo.getCompanyName());
        lqw.like(StringUtils.isNotBlank(bo.getProductEquipmentName()), WbProductEquipment::getProductEquipmentName, bo.getProductEquipmentName());
        lqw.eq(bo.getProductEquipmentModel() != null, WbProductEquipment::getProductEquipmentModel, bo.getProductEquipmentModel());
        lqw.eq(bo.getDataTagging() != null, WbProductEquipment::getDataTagging, bo.getDataTagging());
        lqw.eq(bo.getProductEquipmentNumber() != null, WbProductEquipment::getProductEquipmentNumber, bo.getProductEquipmentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getMainTechnicalParameter()), WbProductEquipment::getMainTechnicalParameter, bo.getMainTechnicalParameter());
        lqw.orderByDesc(WbProductEquipment::getId);
        return lqw;
    }

    /**
     * 新增生产设备
     */
    @Override
    public Boolean insertByBo(WbProductEquipmentBo bo) {
        WbProductEquipment add = MapstructUtils.convert(bo, WbProductEquipment.class);
        validEntityBeforeSave(add);
        Long companyId = add.getCompanyId();
        String companyName=baseMapper.getCompanyName(companyId);
        add.setCompanyName(companyName);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改生产设备
     */
    @Override
    public Boolean updateByBo(WbProductEquipmentBo bo) {
        WbProductEquipment update = MapstructUtils.convert(bo, WbProductEquipment.class);
        validEntityBeforeSave(update);
        Long companyId = update.getCompanyId();
        String companyName=baseMapper.getCompanyName(companyId);
        update.setCompanyName(companyName);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WbProductEquipment entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除生产设备
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
